package com.mocne.Common;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import java.util.HashMap;

public class OperateWithParams {

    private static WebElement findElements(WebDriver webDriver, HashMap<String, String> testData) {
        if (null != testData && !testData.isEmpty()){
            String FindFunc = testData.get("FindFunc");
            String FindParams = testData.get("FindParams");
            String IsPlural = testData.get("IsPlural");
            int IndexNeeded = Integer.valueOf(testData.get("IndexNeeded").substring(0, 1)).intValue();
            if ("xPath".equals(FindFunc)) {
                if ("TRUE".equals(IsPlural)){
                    return webDriver.findElements(By.xpath(FindParams)).get(IndexNeeded);
                } else if ("FALSE".equals(IsPlural)){
                    return webDriver.findElement(By.xpath(FindParams));
                }
            } else if ("ClassName".equals(FindFunc)){
                if ("TRUE".equals(IsPlural)){
                    return webDriver.findElements(By.className(FindParams)).get(IndexNeeded);
                } else if ("FALSE".equals(IsPlural)){
                    return webDriver.findElement(By.className(FindParams));
                }
            } else if ("ID".equals(FindFunc)){
                if ("TRUE".equals(IsPlural)){
                    return webDriver.findElements(By.id(FindParams)).get(IndexNeeded);
                } else if ("FALSE".equals(IsPlural)){
                    return webDriver.findElement(By.id(FindParams));
                }
            }
        }
        return null;
    }

    private static void operateElements(WebElement ele, HashMap<String, String> testData){
        String operate = testData.get("Operation");
        String operateParams = testData.get("OperateParams");
        if ("clear".equals(operate)){
            ele.clear();
        } else if ("click".equals(operate)){
            ele.click();
        } else if ("sendKeys".equals(operate)){
            ele.sendKeys(operateParams);
        } else if ("getText".equals(operate)){
            assert operateParams.equals(ele.getText()): "Element's text is wrong !";
        }
    }

    public static void operateWithParams(WebDriver webDriver,HashMap<String, String> testData){
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if (null != testData && !testData.isEmpty()){
            String operate = testData.get("Operation");
            if ("get".equals(testData.get("Operation"))) {
                try {
                    webDriver.get(testData.get("OperateParams"));
                } catch (Exception e){
                    e.printStackTrace();
                }
            } else if ("wait".equals(operate)){
                try {
                    int waitTime = Integer.parseInt(testData.get("OperateParams").substring(0,1));
                    Thread.sleep(waitTime * 1000);
                } catch (Exception e){
                    e.printStackTrace();
                }
            } else if ("switch".equals(operate)){
                try {
                    webDriver.switchTo().parentFrame();
                } catch (Exception e){
                    e.printStackTrace();
                }
            } else if ("xPath".equals(testData.get("FindFunc")) || "CSS".equals(testData.get("FindFunc")) || "ClassName".equals(testData.get("FindFunc"))){
                try {
                    WebElement ele = OperateWithParams.findElements(webDriver, testData);
                    OperateWithParams.operateElements(ele, testData);
                } catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
    }
}
